package com.example.dbcms.controller.admin;

import com.example.dbcms.annotation.Log;
import com.example.dbcms.domain.User;
import com.example.dbcms.service.UserService;
import com.example.dbcms.utils.ResultVoUtil;
import com.example.dbcms.vo.ResultVo;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.util.FileCopyUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.List;


@Controller
@Api("用户管理")
@RequestMapping("/admin/user")
public class UserController {

    /**
     * 文件上传保存地址
     */
    @Value("${project.upload.file-path}")
    String file_path;

    @Autowired
    private UserService userService;

    /**
     * 跳转到用户列表页面
     * @param user
     * @param modelMap
     * @return
     */
    @GetMapping("/")
    @ApiOperation("跳转到用户列表页面")
    @Log(type="跳转到用户列表页面")
    public String userList(User user, ModelMap modelMap){
        PageInfo<User> userPageInfo = userService.selectAllUser(user);
        modelMap.put("userList",userPageInfo.getList());
        modelMap.put("user",user);
        modelMap.put("pageInfo",userPageInfo);
        return "admin/user/index";
    }

    /**
     * 通过id删除用户
     * @param id
     * @return
     */
    @GetMapping("/deleteUser/{id}")
    @ResponseBody
    @ApiOperation("通过id删除用户")
    @Log(type="通过id删除用户")
    public ResultVo deleteUserById(@PathVariable("id") int id){
        int result = userService.deleteUserById(id);
        if(result == 1){
            return ResultVoUtil.success("删除成功");
        }
        return ResultVoUtil.error("删除失败");
    };

    /**
     * 删除多条用户信息
     * @param ids
     * @return
     */
    @ApiOperation("删除多条用户信息")
    @PostMapping("/deleteMultiple")
    @ResponseBody
    @Log(type="删除多条用户信息")
    public ResultVo deleteMultipleUserBYId(@RequestParam(value = "ids") List<Integer> ids){
        int result = userService.deleteMultipleUserById(ids);
        if(result == ids.size()){
            return ResultVoUtil.success("删除成功");
        }
        return ResultVoUtil.error("删除失败");
    };

    /**
     * 通过id跳转到详细用户页面
     * @param id
     * @param modelMap
     * @return
     */
    @GetMapping("/detailUser/{id}")
    @ApiOperation("通过id跳转到详细用户页面")
    @Log(type="通过id跳转到详细用户页面")
    public String detailUserById(@PathVariable("id") int id,ModelMap modelMap){
        modelMap.put("user",userService.getUserById(id));
        return "admin/user/detail";
    };

    /**
     * 通过id跳转到编辑页面
     * @param id
     * @param modelMap
     * @return
     */
    @GetMapping("/editUser/{id}")
    @ApiOperation("通过id跳转到编辑页面")
    @Log(type="通过id跳转到编辑页面")
    public String editUserNyId(@PathVariable("id") int id,ModelMap modelMap){
        modelMap.put("user",userService.getUserById(id));
        return "admin/user/edit";
    }

    /**
     * 修改用户数据到数据库
     * @param user
     * @return
     */
    @PostMapping("/editUser")
    @ResponseBody
    @ApiOperation("修改用户数据到数据库")
    @Log(type="修改用户数据到数据库")
    public ResultVo saveUserById(User user){
        System.out.println(user);
        int result = userService.updateUserById(user);
        if(result == 1){
            return ResultVoUtil.success("修改成功");
        }
        return ResultVoUtil.error("修改失败");
    }

    /**
     * 跳转到新增用户页面
     * @return
     */
    @GetMapping("/addUser")
    @ApiOperation("跳转到新增用户页面")
    @Log(type="跳转到新增用户页面")
    public String addUser(){
        return "admin/user/add";
    }

    /**
     * 新增用户数据到数据库
     * @param user
     * @return
     */
    @PostMapping("/addUser")
    @ResponseBody
    @ApiOperation("新增用户数据到数据库")
    @Log(type="新增用户数据到数据库")
    public ResultVo addUserToDb(User user){
        int result = userService.addUser(user);
        if(result == 1){
            return ResultVoUtil.success("添加成功");
        }
        return ResultVoUtil.error("添加失败");
    }
    /**
     * 获取用户头像
     */
    @GetMapping("/picture")
    @ApiOperation("获取用户头像")
    @Log(type="获取用户头像")
    public void picture(String fileName, HttpServletResponse response) throws IOException {
        String defaultPath = "/img/user-picture.jpg";
        Resource resource = new ClassPathResource("static" + defaultPath);
        File file = new File(file_path+fileName);
        if (file.exists()) {
            FileCopyUtils.copy(new FileInputStream(file), response.getOutputStream());
            return;
        }
        FileCopyUtils.copy(resource.getInputStream(), response.getOutputStream());
    }
}
